package org.lionsoul.websnail.store;

import java.util.LinkedList;


/**
 * Spider todo list base one linked-list (Thread safe)
 *
 * @author chenxin<chenxin619315 @ gmail.com>
 */

public class LinkedListTodo implements Todo {

    LinkedList<String> list;

    public LinkedListTodo() {
        list = new LinkedList<>();
    }

    @Override
    public synchronized boolean add(String url) {
        return list.add(url);
    }

    @Override
    public synchronized boolean add(String[] urls) {
        for (String url : urls) {
            if (add(url) == false) {
                return false;
            }
        }

        return true;
    }

    @Override
    public synchronized boolean addFirst(String url) {
        list.addFirst(url);
        return true;
    }

    @Override
    public synchronized boolean addFirst(String[] urls) {
        for (String url : urls) {
            if (addFirst(url) == false) {
                return false;
            }
        }
        return true;
    }

    @Override
    public synchronized void clear() {
        list.clear();
    }

    /**
     * @see Todo#shift()
     */
    @Override
    public synchronized String shift() {
        if (list.size() == 0) {
            return null;
        }
        return list.removeFirst();
    }

    @Override
    public synchronized String pop() {
        if (list.size() == 0) {
            return null;
        }
        return list.removeLast();
    }

    @Override
    public synchronized int size() {
        return list.size();
    }

    @Override
    public void close() {
        list.clear();
        list = null;
    }
}
